<template>
	<view class="new-users copy-data">
		<!-- 筛选条件和搜索框 -->
		<view class="searchBox">
			<view class="filterBox">
				<picker @change="onFilterChange" :value="filterIndex" :range="filterOptions" class="filterPicker">
					<view class="filterText">{{ filterOptions[filterIndex] }}</view>
				</picker>
			</view>
			<uni-search-bar ref="searchBar" radius="30" placeholder="请输入关键字" clearButton="auto" cancelButton="none"
				@confirm="onSearchConfirm" class="searchInput" />
		</view>

		<!-- 营业额信息 -->
		<view class="moneyBox">
			<view class="topMoney">
				<text style="text-align: left;">总营业额</text>
				<text style="text-align: right;color: #1D85E1;">{{statistics.totalSales}}</text>
			</view>
			<view class="money-details">
				<p class="p1">今日营业额</p>
				<p class="p1">今日订单数</p>
				<p class="p1">今日返利</p>
				<p class="p2">{{statistics.todaySales || 0}}</p>
				<p class="p2">{{statistics.todayOrder || 0}}</p>
				<p class="p2">{{statistics.todayRebate || 0}}</p>
			</view>
		</view>

		<view class="nav">
			<uni-datetime-picker v-model="datetimerange" type="datetimerange" mode="daterange" rangeSeparator="至"
				@change="handleConfirm">
				<view class="fold" @click="openPicker">时间筛选</view>
			</uni-datetime-picker>
		</view>

		<!-- 订单 列表 -->
		<view class="orderBox" v-for="(item, index) in orderList" :key="index">
			<view class="topUser">
				<view style="width: 40%;">
					<p>{{ item.msqUser.nickname }}</p>
					<p>UID：{{ item.msqUser.uid }}</p>
				</view>
				<view style="width: 40%; color: #8F5A31;text-align: right;">
					{{ item.msqUser.account }}
				</view>
			</view>
			<view class="bottomMoney">
				<p>商品名称：{{ item.msqUserGoods[0].productName }}</p>
				<p>佣金发放：{{ item.commission }}</p>
				<p>流水抽成：{{ item.branchProportion }}</p>
				<p>订单金额：{{ item.msqUserGoods[0].price }}</p>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		getUserInfo
	} from '@/api/user.js';
	import {
		getStorePage,
		getStatistics
	} from '@/api/business.js';

	export default {
		data() {
			return {
				userInfo: {},
				statistics: {},
				datetimerange: ['', ''], // 初始值为空数组
				orderList: [],
				pageSize: 10, // 每页显示条目数
				pageNum: 1, // 当前页码
				beginTime: '',
				endTime: '',
				searchKeyword: null, // 搜索关键字
				filterIndex: 0, // 筛选条件索引，默认为 userId
				filterOptions: ['按用户ID', '按昵称', '按手机号'] // 筛选条件选项
			};
		},
		onLoad() {
			this.getUserDetail();
		},
		methods: {
			openPicker() {
				// 触发日期选择器的显示
				this.$refs.datetimePicker.show();
			},
			handleConfirm(e) {
				this.pageNum = 1;
				this.pageSize = 10;
				this.getOrderList();
			},
			getUserDetail() {
				getUserInfo().then((res) => {
					this.userInfo = res.data;
					this.getOrderList(this.userInfo.msqStore.id);
					this.getStatisticsDetail(this.userInfo.msqStore.id)
				});
			},
			getStatisticsDetail(id) {
				getStatistics(id).then(res => {
					this.statistics = res.data;
				});
			},
			async getOrderList() {
				console.log(this.filterIndex, this.searchKeyword, "searchKeyword")
				const data = {
					storeId: this.userInfo.msqStore.id,
					pageSize: this.pageSize,
					pageNum: this.pageNum,
					beginTime: null,
					endTime: null,
					userId: this.filterIndex == 0 ? this.searchKeyword : null,
					nickName: this.filterIndex == 1 ? this.searchKeyword : null,
					phone: this.filterIndex == 2 ? this.searchKeyword : null,
				};

				// 定义一个函数来替换 %20 为 空格
				const replaceSpaces = (timeString) => {
					if (!timeString) return null;
					return timeString.replace(/%20/g, ' ');
				};

				// 如果开始时间存在，则设置时间范围参数
				if (this.datetimerange && this.datetimerange[0]) {
					data.beginTime = replaceSpaces(this.datetimerange[0]);
					data.endTime = replaceSpaces(this.datetimerange[1] || '');
				}

				try {
					const res = await getStorePage(data);
					const newList = res.data.records || []; // 假设返回的数据结构有一个records字段
					// 追加到现有列表
					this.orderList = this.orderList.concat(newList);
					// 更新当前页码
					this.pageNum++;
				} catch (error) {
					console.error('获取订单列表失败:', error);
				}
			},
			onFilterChange(e) {
				// 保存筛选条件索引并清除搜索关键字
				this.filterIndex = e.detail.value;
				this.searchKeyword = '';
				this.$refs.searchBar.clear(); // 调用 uni-search-bar 的 clear 方法清空输入框
			},

			onSearchConfirm(e) {
				// 保存搜索关键字并重置分页
				this.searchKeyword = e.value;
				this.pageNum = 1;
				this.orderList = [];
				this.getOrderList();
			}
		},
		onReachBottom() {
			// 触底加载更多
			this.getOrderList(this.userInfo.msqStore.id);
		}
	};
</script>
<style lang="scss" scoped>
	page,
	body {
		height: 100%;
	}

	.searchBox {
		background-color: #fff;
		padding: 20rpx;
		display: flex;
		align-items: center;

		.filterBox {
			margin-right: 20rpx;
		}

		.filterPicker {
			width: 150rpx; // 设置筛选条件选择器的宽度
		}

		.searchInput {
			flex-grow: 1;
		}
	}

	.moneyBox {
		margin: 30rpx;
		display: flex;
		flex-wrap: wrap;
		padding: 36rpx 43rpx;
		background: linear-gradient(24deg, #FDFBF9 0%, #F7F2EB 100%);

		.topMoney {
			width: 100%;
			border-bottom: 2rpx solid #1D85E1;
			padding-bottom: 20rpx;
			font-weight: bold;
			font-size: 34rpx;
			display: flex;
			justify-content: space-between;
		}

		.money-details {
			width: 100%;
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 10rpx;
			margin-top: 20rpx;
			text-align: center;

			.p1 {
				font-weight: 400;
				font-size: 26rpx;
				color: #666666;
			}

			.p2 {
				font-weight: 800;
				font-size: 38rpx;
				color: #1D85E1;
			}
		}
	}

	.nav {
		background-color: #fff;
		height: 86rpx;
		line-height: 86rpx;
		font-size: 28rpx;
		color: #000;
		border-bottom: 1rpx solid #EEEEEE;
		border-top-left-radius: 14rpx;
		border-top-right-radius: 14rpx;
	}

	.fold {
		float: right;
		margin-right: 40rpx;
	}

	.orderBox {
		display: flex;
		flex-direction: column;
		padding: 36rpx 43rpx;
		margin: 30rpx;
		border-radius: 20rpx;
		background: #fff;

		.topUser {
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 2rpx solid #1D85E1;
			padding-bottom: 10rpx;

			.avatar {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
			}

			p {
				margin: 0;
			}
		}

		.bottomMoney {
			display: flex;
			flex-wrap: wrap;
			color: #666666;
			background-color: #F6F6F6;
			padding: 10rpx;

			p {
				width: 50%;
				margin: 10rpx 0rpx;
			}
		}
	}

	.loading {
		text-align: center;
		color: #999;
		padding: 20rpx;
	}

	.no-more {
		text-align: center;
		color: #ccc;
		padding: 20rpx;
	}
</style>